package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.SysChina;
import com.ruoyi.system.mapper.SysChinaMapper;
import com.ruoyi.system.service.ISysChinaService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("chinaService")
public class SysChinaServiceImpl extends ServiceImpl<SysChinaMapper, SysChina> implements ISysChinaService {
    @Override
    public List<Map<String, Object>> getCityTree() {
        QueryWrapper<SysChina> qw = new QueryWrapper<SysChina>();
        qw.eq("pid", 0);
        qw.ne("id", 0);
        List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
        List<SysChina> list = this.list(qw);
        for (SysChina city : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", city.getName());
            map.put("code", city.getId());
            //map.put("children", this.getChild(city.getId()));
            result.add(map);
        }
        return result;
    }

    @Override
    public List<Map<String, Object>> getChild(Integer id) {
        System.out.println(id);
        QueryWrapper<SysChina> qw = new QueryWrapper<SysChina>();
        qw.eq("pid", id);
        List<Map<String, Object>> result = new ArrayList<Map<String,Object>>();
        List<SysChina> list = this.list(qw);
        for (SysChina city : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("name", city.getName());
            map.put("code", city.getId());
            //map.put("children", this.getChild(city.getId()));
            result.add(map);
        }
        return result;
    }
}
